﻿using Learun.Application.Base.SystemModule;
using Learun.Util;
using Learun.Util.SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-01-30 15:57
    /// 描 述：图纸文件
    /// </summary>
    public class ec_drawing_fileService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_drawing_fileEntity> _drawingFileRepository => new Repository<ec_drawing_fileEntity>();
        Repository<ec_operate_logEntity> _operateLogRepository => new Repository<ec_operate_logEntity>();
        Repository<ec_drawing_synEntity> _drawingSynRepository => new Repository<ec_drawing_synEntity>();
        Repository<ec_drawing_publishEntity> _drawingPublishRepository => new Repository<ec_drawing_publishEntity>();
        Repository<AnnexesFileEntity> _annexesFileRepository => new Repository<AnnexesFileEntity>();
        Repository<ec_enginedataEntity> _enginedataRepository => new Repository<ec_enginedataEntity>();
        Repository<ec_enginedata_pixelEntity> _enginedataPixelRepository => new Repository<ec_enginedata_pixelEntity>();
        Repository<ec_enginedata_propertyEntity> _enginedataPropertyRepository => new Repository<ec_enginedata_propertyEntity>();
        #endregion
        #region 获取数据

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_drawing_fileEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append(@"SELECT t1.*
                                    ,t2.F_RealName CreateUserName
                                    ,t3.F_RealName CheckOutUserName
                                    ,t4.F_RealName UpdateUserName
                                    ,t5.DataItemName DrawingTypeName
                                    ,t6.DataItemName DrawingStageName
                                    ,t7.DataItemName DrawingSystemName
                                    ,t8.DataItemName MaterialCensusName
                                    ,t9.DataItemName MaterialRangeName
                                ");
                //var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                var ProjectEntity = _projectRepository.GetById(ProjectId);
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    strSql.Append("  FROM ec_drawing_file t1 ");
                }
                else
                {
                    strSql.Append($"  FROM ec_drawing_file_{ProjectEntity.ProjectIndex} t1 ");
                }
                strSql.Append($@"LEFT JOIN lr_base_user t2 ON t1.CreateUserID = t2.F_UserId
                                LEFT JOIN lr_base_user t3 ON t1.CheckOutUserID = t3.F_UserId
                                LEFT JOIN lr_base_user t4 ON t1.UpdateUserID = t4.F_UserId
                                 LEFT JOIN ec_dataitemdetail_{ProjectEntity.ProjectIndex} t5 ON t5.DataItemDetailID = t1.DrawingType AND t5.DataItemID IN (SELECT st1.DataItemID FROM ec_dataitem_{ProjectEntity.ProjectIndex} st1 WHERE st1.DataItemCode= 'Be_DrawingType') 
                                 LEFT JOIN ec_dataitemdetail_{ProjectEntity.ProjectIndex} t6 ON t6.DataItemDetailID = t1.DrawingStage AND t6.DataItemID IN (SELECT st1.DataItemID FROM ec_dataitem_{ProjectEntity.ProjectIndex} st1 WHERE st1.DataItemCode= 'Be_DrawingStage') 
                                 LEFT JOIN ec_dataitemdetail_{ProjectEntity.ProjectIndex} t7 ON t7.DataItemDetailID = t1.DrawingSystem AND t7.DataItemID IN (SELECT st1.DataItemID FROM ec_dataitem_{ProjectEntity.ProjectIndex} st1 WHERE st1.DataItemCode= 'Be_DrawingSystem')
                                 LEFT JOIN ec_dataitemdetail_{ProjectEntity.ProjectIndex} t8 ON t8.DataItemDetailID = t1.MaterialCensus AND t8.DataItemID IN (SELECT st1.DataItemID FROM ec_dataitem_{ProjectEntity.ProjectIndex} st1 WHERE st1.DataItemCode= 'Be_MaterialCensus') 
                                 LEFT JOIN ec_dataitemdetail_{ProjectEntity.ProjectIndex} t9 ON t9.DataItemDetailID = t1.MaterialRange AND t9.DataItemID IN (SELECT st1.DataItemID FROM ec_dataitem_{ProjectEntity.ProjectIndex} st1 WHERE st1.DataItemCode= 'Be_MaterialRange') 
                                WHERE 1=1 ");
                strSql.Append(" AND t1.DrawingFileID = @DrawingFileID ");
                //List<ec_drawing_fileEntity> list = (List<ec_drawing_fileEntity>)this.BaseRepository().FindList<ec_drawing_fileEntity>(strSql.ToString(), new { DrawingFileID = keyValue }).ToList();
                //return list.FirstOrDefault();
                return SqlSugarHelper.Db.Ado.SqlQuerySingle<ec_drawing_fileEntity>(strSql.ToString(), new { DrawingFileID = keyValue });
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(keyValue, ProjectId);
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    //db.Delete<ec_drawing_fileEntity>(t => t.DrawingFileID == keyValue);
                    _drawingFileRepository.Delete(t => t.DrawingFileID == keyValue);
                }
                else
                {
                    //var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(ProjectId);
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_file_{ProjectEntity.ProjectIndex} WHERE DrawingFileID = @DrawingFileID  ", new { DrawingFileID = keyValue });
                    SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_publish_{ProjectEntity.ProjectIndex} WHERE DrawingFileID = @DrawingFileID  ", new { DrawingFileID = keyValue });
                    SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_drawing_syn_{ProjectEntity.ProjectIndex} WHERE DrawingFileID = @DrawingFileID  ", new { DrawingFileID = keyValue });
                    SqlSugarHelper.Db.Ado.ExecuteCommand($" DELETE FROM ec_enginedata_pixel_{ProjectEntity.ProjectIndex} WHERE DrawingFileID = @DrawingFileID  ", new { DrawingFileID = keyValue });
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 单一更新实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void UpdataEntity(ec_drawing_fileEntity entity, string ProjectId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingFileRepository.Update(entity);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    //this.BaseRepository().UpdateByNoMap(entity, $"ec_drawing_file_{ProjectEntity.ProjectIndex}", "DrawingFileID");
                    _drawingFileRepository.AsUpdateable(entity).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_drawing_fileEntity entity, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.Create();
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entityLog.OperateType = "编辑";
                    entityLog.FileVersion = entity.FileVersion;
                    entity.Modify(keyValue);
                    entityLog.OwnerID = entity.DrawingFileID;
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _drawingFileRepository.Update(entity);
                        _operateLogRepository.Insert(entityLog);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //db.UpdateByNoMap(entity, $"ec_drawing_file_{ProjectEntity.ProjectIndex}", "DrawingFileID");
                        //db.InsertByNoMap(entityLog, $"ec_operate_log_{ProjectEntity.ProjectIndex}");
                        _drawingFileRepository.AsUpdateable(entity).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                        _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }

                }
                else
                {
                    entityLog.OperateType = "新增";
                    entity.Create();
                    entityLog.FileVersion = entity.FileVersion;
                    entityLog.OwnerID = entity.DrawingFileID;
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _drawingFileRepository.Insert(entity);
                        _operateLogRepository.Insert(entityLog);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //db.InsertByNoMap(entity, $"ec_drawing_file_{ProjectEntity.ProjectIndex}");
                        //db.InsertByNoMap(entityLog, $"ec_operate_log_{ProjectEntity.ProjectIndex}");
                        _drawingFileRepository.AsInsertable(entity).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                        _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 新增
        /// <summary>
        /// <returns></returns>
        public void InertEntity(ec_drawing_fileEntity entity, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entity.DrawingFileID;
                entityLog.OperateType = "新增";
                entityLog.FileVersion = entity.FileVersion;
                entityLog.Create();
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingFileRepository.Insert(entity);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    _drawingFileRepository.AsInsertable(entity).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 编辑
        /// <summary>
        /// <returns></returns>
        public void EditEntity(ec_drawing_fileEntity entity, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                entity.Modify(entity.DrawingFileID);
                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entity.DrawingFileID;
                entityLog.OperateType = "编辑";
                entityLog.FileVersion = entity.FileVersion;
                entityLog.Create();
                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingFileRepository.Update(entity);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    _drawingFileRepository.AsUpdateable(entity).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 检出
        /// <summary>
        /// <returns></returns>
        public void CheckOutDrawingFile(string drawingFileID, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(drawingFileID, ProjectId);
                //更新图纸文件表
                entityFile.IsCheckOut = 1;
                entityFile.CheckOutUserID = LoginUserInfo.Get().userId;
                entityFile.CheckOutTime = Time.MySqlTime;
                entityFile.Modify(drawingFileID);

                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entityFile.DrawingFileID;
                entityLog.OperateType = "检出";
                entityLog.FileVersion = entityFile.FileVersion;
                entityLog.Create();

                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingFileRepository.Update(entityFile);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    _drawingFileRepository.AsUpdateable(entityFile).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 检入
        /// <summary>
        /// <returns></returns>
        [Obsolete]
        public void CheckInDrawingFile(string drawingFileID, string remark, string folderId, string fileMD5, string ProjectId, string UsersNotified)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(drawingFileID, ProjectId);


                //新增图纸同步表
                ec_drawing_synEntity entitySyn = new ec_drawing_synEntity();
                entitySyn.DrawingFileID = drawingFileID;
                entitySyn.FileVersion = entityFile.FileVersion;
                entitySyn.Remark = remark;
                entitySyn.FolderId = folderId;
                entitySyn.Create();

                //更新图纸文件表
                entityFile.IsCheckOut = 0;
                entityFile.CheckOutUserID = null;//感觉最好弄到checkindrawingfile这个接口里
                entityFile.CheckOutTime = null;
                entityFile.FolderId = folderId;
                entityFile.FileMD5 = fileMD5;
                entityFile.Modify(drawingFileID);

                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entityFile.DrawingFileID;
                entityLog.OperateType = "检入";
                entityLog.FileVersion = entityFile.FileVersion;
                entityLog.Create();

                //新增通知表
                var notificationBLL = new ec_notificationBLL();
                if (UsersNotified != null)
                {
                    foreach (var userName in UsersNotified.Replace("|", ",").Replace("，", ",").Split(','))
                    {
                        ec_notificationEntity ec_NotificationEntity = new ec_notificationEntity();
                        ec_NotificationEntity.SenderUserID = LoginUserInfo.Get().realName;
                        ec_NotificationEntity.RetrieveUserID = userName;
                        ec_NotificationEntity.DrawingSynID = entitySyn.DrawingSynID;
                        ec_NotificationEntity.CheckFLG = 0;
                        notificationBLL.SaveEntity("", ec_NotificationEntity, ProjectId);
                    }

                }

                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingSynRepository.Insert(entitySyn);
                    _drawingFileRepository.Update(entityFile);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    //db.InsertByNoMap(entitySyn, $"ec_drawing_syn_{ProjectEntity.ProjectIndex}");
                    //db.UpdateByNoMap(entityFile, $"ec_drawing_file_{ProjectEntity.ProjectIndex}", "DrawingFileID");
                    //db.InsertByNoMap(entityLog, $"ec_operate_log_{ProjectEntity.ProjectIndex}");
                    _drawingSynRepository.AsInsertable(entitySyn).AS($"ec_drawing_syn_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _drawingFileRepository.AsUpdateable(entityFile).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 检入
        /// <summary>
        /// <returns></returns>
        public void CheckInDrawingFileNew(ec_drawing_synEntity obj, string fileMD5, string ProjectId, string UsersNotified)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(obj.DrawingFileID, ProjectId);


                //新增图纸同步表
                ec_drawing_synEntity entitySyn = obj;
                entitySyn.Create();

                //更新图纸文件表
                entityFile.IsCheckOut = 0;
                entityFile.CheckOutUserID = null;//感觉最好弄到checkindrawingfile这个接口里
                entityFile.CheckOutTime = null;
                entityFile.FolderId = obj.FolderId;
                entityFile.FileMD5 = fileMD5;
                entityFile.Modify(obj.DrawingFileID);

                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entityFile.DrawingFileID;
                entityLog.OperateType = "检入";
                entityLog.FileVersion = entityFile.FileVersion;
                entityLog.Create();

                //新增通知表
                var notificationBLL = new ec_notificationBLL();
                if (UsersNotified != null)
                {
                    foreach (var userName in UsersNotified.Replace("|", ",").Replace("，", ",").Split(','))
                    {
                        ec_notificationEntity ec_NotificationEntity = new ec_notificationEntity();
                        ec_NotificationEntity.SenderUserID = LoginUserInfo.Get().realName;
                        ec_NotificationEntity.RetrieveUserID = userName;
                        ec_NotificationEntity.DrawingSynID = entitySyn.DrawingSynID;
                        ec_NotificationEntity.CheckFLG = 0;
                        notificationBLL.SaveEntity("", ec_NotificationEntity, ProjectId);
                    }

                }

                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingSynRepository.Insert(entitySyn);
                    _drawingFileRepository.Update(entityFile);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    //db.InsertByNoMap(entitySyn, $"ec_drawing_syn_{ProjectEntity.ProjectIndex}");
                    //db.UpdateByNoMap(entityFile, $"ec_drawing_file_{ProjectEntity.ProjectIndex}", "DrawingFileID");
                    //db.InsertByNoMap(entityLog, $"ec_operate_log_{ProjectEntity.ProjectIndex}");
                    _drawingSynRepository.AsInsertable(entitySyn).AS($"ec_drawing_syn_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _drawingFileRepository.AsUpdateable(entityFile).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 释放
        /// <summary>
        /// <returns></returns>
        public void FreeDrawingFile(string drawingFileID, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(drawingFileID, ProjectId);
                //更新图纸文件表
                entityFile.IsCheckOut = 0;
                entityFile.CheckOutUserID = null;
                entityFile.CheckOutTime = null;
                entityFile.Modify(drawingFileID);

                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entityFile.DrawingFileID;
                entityLog.OperateType = "释放";
                entityLog.FileVersion = entityFile.FileVersion;
                entityLog.Create();

                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingFileRepository.Update(entityFile);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    _drawingFileRepository.AsUpdateable(entityFile).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 发布
        /// <summary>
        /// <returns></returns>
        public void PublishDrawingFile(string drawingFileID, string remark, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(drawingFileID, ProjectId);
                //新增图纸同步表
                ec_drawing_publishEntity entityPublish = new ec_drawing_publishEntity();
                entityPublish.DrawingFileID = drawingFileID;
                entityPublish.FileVersion = entityFile.FileVersion;
                entityPublish.Remark = remark;
                entityPublish.FolderId = entityFile.FolderId;
                entityPublish.Create();

                //更新图纸文件表
                entityFile.IsPublish = 1;
                entityFile.CheckOutUserID = null;
                entityFile.CheckOutTime = null;
                entityFile.Modify(drawingFileID);

                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entityFile.DrawingFileID;
                entityLog.OperateType = "发布";
                entityLog.FileVersion = entityFile.FileVersion;
                entityLog.Create();

                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingPublishRepository.Insert(entityPublish);
                    _drawingFileRepository.Update(entityFile);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    //db.InsertByNoMap(entityPublish, $"ec_drawing_publish_{ProjectEntity.ProjectIndex}");
                    //db.UpdateByNoMap(entityFile, $"ec_drawing_file_{ProjectEntity.ProjectIndex}", "DrawingFileID");
                    //db.InsertByNoMap(entityLog, $"ec_operate_log_{ProjectEntity.ProjectIndex}");
                    _drawingPublishRepository.AsInsertable(entityPublish).AS($"ec_drawing_publish_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _drawingFileRepository.AsUpdateable(entityFile).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 修订
        /// <summary>
        /// <returns></returns>
        public void ModifyDrawingFile(string drawingFileID, string fileVersion, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                ec_drawing_fileEntity entityFile = GetEntity(drawingFileID, ProjectId);
                //更新图纸文件表
                entityFile.IsCheckOut = 1;
                entityFile.CheckOutUserID = LoginUserInfo.Get().userId;
                entityFile.CheckOutTime = Time.MySqlTime;
                entityFile.IsPublish = 0;
                //版本
                entityFile.FileVersion = fileVersion;
                entityFile.Modify(drawingFileID);

                //操作日志
                ec_operate_logEntity entityLog = new ec_operate_logEntity();
                entityLog.OwnerID = entityFile.DrawingFileID;
                entityLog.OperateType = "修订";
                entityLog.FileVersion = entityFile.FileVersion;
                entityLog.Create();

                if (string.IsNullOrWhiteSpace(ProjectId))
                {
                    _drawingFileRepository.Update(entityFile);
                    _operateLogRepository.Insert(entityLog);
                }
                else
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    _drawingFileRepository.AsUpdateable(entityFile).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _operateLogRepository.AsInsertable(entityLog).AS($"ec_operate_log_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存图纸导入数据
        /// </summary>
        /// <param name="addDrawingFileList">新增的图纸集合</param>
        /// <param name="addAnnexesFileList">新增的附件集合</param>
        /// <param name="addEngineDataList">新增的工程数据</param>
        /// <param name="updateEngineDataList">更新的工程数据</param>
        /// <param name="addPixelList">新增的图元集合</param>
        /// <param name="addPropertyList">新增的属性集合</param>
        /// <param name="listPropByEngineIds">要删除的属性表中工程数据ID集合</param>
        public void SaveSelectData(List<ec_drawing_fileEntity> addDrawingFileList,
                                    List<AnnexesFileEntity> addAnnexesFileList,
                                    List<ec_enginedataEntity> addEngineDataList,
                                    List<ec_enginedataEntity> updateEngineDataList,
                                    List<ec_enginedata_pixelEntity> addPixelList,
                                    List<ec_enginedata_propertyEntity> addPropertyList,
                                    List<string> listPropByEngineIds, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                if (!string.IsNullOrEmpty(ProjectId))
                {
                    string delWhere = "";
                    var ProjectEntity = _projectRepository.GetById(ProjectId);

                    //工程数据属性表
                    if (listPropByEngineIds != null && listPropByEngineIds.Count > 0)
                    {
                        string strWhere = string.Join(",", (from tb in listPropByEngineIds select "'" + tb + "'").ToList());
                        delWhere += $" DELETE FROM ec_enginedata_property_{ProjectEntity.ProjectIndex} WHERE EngineDataID IN ({strWhere}); ";
                    }
                    if (!string.IsNullOrEmpty(delWhere))
                    {
                        //db.ExecuteBySql(delWhere);
                        SqlSugarHelper.Db.Ado.ExecuteCommand(delWhere);
                    }
                    //db.InsertByNoMap(addDrawingFileList, $"ec_drawing_file_{ProjectEntity.ProjectIndex}");
                    //db.Insert(addAnnexesFileList);
                    //db.InsertByNoMap(addEngineDataList, $"ec_enginedata_{ProjectEntity.ProjectIndex}");
                    //db.UpdateByNoMap(updateEngineDataList, $"ec_enginedata_{ProjectEntity.ProjectIndex}", "EngineDataID");
                    //db.InsertByNoMap(addPixelList, $"ec_enginedata_pixel_{ProjectEntity.ProjectIndex}");
                    //db.InsertByNoMap(addPropertyList, $"ec_enginedata_property_{ProjectEntity.ProjectIndex}");
                    _drawingFileRepository.AsInsertable(addDrawingFileList).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _annexesFileRepository.InsertRange(addAnnexesFileList);
                    _enginedataRepository.AsInsertable(addEngineDataList).AS($"ec_enginedata_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _enginedataRepository.AsUpdateable(updateEngineDataList).AS($"ec_enginedata_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _enginedataPixelRepository.AsInsertable(addPixelList).AS($"ec_enginedata_pixel_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _enginedataPropertyRepository.AsInsertable(addPropertyList).AS($"ec_enginedata_property_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 修改集合数据,新增附件数据集合
        /// <summary>
        /// <returns></returns>
        public void SaveDataAndAnnexes(List<ec_drawing_fileEntity> entityList, List<AnnexesFileEntity> listAnnexesFile, string ProjectId)
        {
            SqlSugarHelper.Db.BeginTran();
            try
            {
                if (!string.IsNullOrEmpty(ProjectId))
                {
                    var ProjectEntity = _projectRepository.GetById(ProjectId);
                    //db.UpdateByNoMap(entityList, $"ec_drawing_file_{ProjectEntity.ProjectIndex}", "DrawingFileID");
                    //db.Insert(listAnnexesFile);
                    _drawingFileRepository.AsUpdateable(entityList).AS($"ec_drawing_file_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    _annexesFileRepository.InsertRange(listAnnexesFile);
                }
                SqlSugarHelper.Db.CommitTran();
            }
            catch (Exception ex)
            {
                SqlSugarHelper.Db.RollbackTran();
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        #endregion

    }
}
